package servlet.admin;

import com.google.gson.Gson;
import entity.Pet;
import service.Impl.PetServiceImpl;
import service.PetService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = "/admin/PetFindByName")
public class PetFindByNameServlet extends HttpServlet {
    private PetService petService = new PetServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String petName = req.getParameter("petName");
        List<Pet> petList = petService.findPetByName(petName);

        // 设置响应内容类型为 JSON
        resp.setContentType("application/json");
        resp.setCharacterEncoding("UTF-8");

        // 将宠物列表转换为 JSON 格式并写入响应体
        String json = new Gson().toJson(petList);
        resp.getWriter().write(json);
    }
}
